﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Common;
using Common.EventHandler;
using Domain;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace MapComponentSprite
{
    public class BuildingUnitSprite : UserControlledSprite
    {
        public static List<BuildingUnitSprite> BuildingUnitSprites = new List<BuildingUnitSprite>();
        public short Id;
        public Building Building;

        private BuildingTypeSprite _buildingTypeSprite = new BuildingTypeSprite();
        private BuildingNameSprite _buildingNameSprite = new BuildingNameSprite();
        private FlagSprite _flagSprite;
        private static EmperorSprite _emperorSprite;//天子是单例模式

        public BuildingUnitSprite(short id, Building building)
        {
            this.Id = id;
            this.Building = building;
            this.Showing = true;
            _flagSprite = new FlagSprite(Building.Force == null ? null : Building.Force.FirstName);
            if (_emperorSprite == null)//天子是单例模式
                _emperorSprite = new EmperorSprite();

        }
        public override void Draw(GameTime gameTime)
        {
            if (!Showing)
                return;
            _buildingTypeSprite.Draw(gameTime );
            _buildingNameSprite.Draw(gameTime );
            _flagSprite.Draw(gameTime ,Building.Force == null ? Color.White : Building.Force.Color);//有些城池不属于某个势力，则为白色旗子
            if (Building.HasEmper) //有天子的城池才需要绘制
                _emperorSprite.Draw(gameTime );

        }

        public override void Update(GameTime gameTime)
        {
            if (!Showing)
                return;
            _buildingTypeSprite.Update(gameTime, Building.CityTypeId, Building.CityLevel, Building.Position);
            _buildingNameSprite.Update(gameTime, new Point(_buildingTypeSprite.DrawPosition.X, _buildingTypeSprite.DrawPosition.Y),
                Building.NumberName);
            _flagSprite.Update(gameTime, Building.Position);
            if (Building.HasEmper)
                _emperorSprite.Update(gameTime, Building.Position);
            //设置本城池鼠标单击坐标范围，方便事件监听ClickLeftEventListener判断
            DrawPosition.X = _buildingTypeSprite.DrawPosition.X;
            DrawPosition.Y = _buildingTypeSprite.DrawPosition.Y;
            DrawPosition.Width = _buildingTypeSprite.DrawPosition.Width;
            DrawPosition.Height = _buildingTypeSprite.DrawPosition.Height;
            ClickLeftEventListener("1," + DrawPosition.X + "|" + DrawPosition.Y+","+this.Building.Id);
            MoveOnEventListener("1,"+Building.Id+"," + DrawPosition.X + "|" + DrawPosition.Y);
            MoveOutEventListener("0");
        }
        public static void LoaMapUnitSpritesData()
        {
            if (BuildingUnitSprites.Count > 0)
                return;
            foreach (var b in Building.Buildings)
            {
                BuildingUnitSprite s = new BuildingUnitSprite(b.Key, b.Value);
                BuildingUnitSprites.Add(s);
            }
        }


    }
}
